package com.hunter.api.mapper;

import com.alibaba.fastjson.JSONObject;

import com.hunter.api.bean.UserBean;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * @author HunterHuang
 * @version 1.0
 * @ClassName UserMapper
 * @date 2019/5/30 10:12
 */
@Repository
public interface UserMapper {
    @Select("SELECT * FROM app.sys_user where id = ${id} and is_deleted = 0 and enabled =1")
    @Results
    UserBean userInfo(Long paramLong);

    @Select("SELECT * FROM app.sys_user where username = #{username} or phone = #{phone} limit 1")
    @Results
    UserBean userInfoName(UserBean paramUserBean);

    @Select("SELECT * FROM app.sys_user WHERE (username = #{username} or open_id = #{open_id}) and password = #{password} and is_deleted = 0 and enabled=1 ")
    @Results
    UserBean LoginInfo(UserBean paramUserBean);

    @Select("SELECT id,COALESCE(u.phone,'') as phone,username,nickname,COALESCE(name,'') as name,COALESCE(img_url,'') as img_url FROM app.sys_user as u WHERE (username = #{username} or #{username} = '') and (phone = #{phone} or #{phone} = '') and password = #{password} and is_deleted = 0 and enabled=1 ")
    @Results
    JSONObject Login(UserBean paramUserBean);

    @Insert("INSERT INTO app.sys_user (ordinal,enabled,created_time,created_by,updated_time,updated_by,deleted_time,deleted_by,is_deleted,username,nickname,NAME,img_url,note,PASSWORD,phone,email,need_changepwd,permissions,staff_id,ext_fields,pwd_updated_time,lang,theme,theme_color ) VALUES(#{ordinal},#{enabled},#{created_time},#{created_by},#{updated_time},#{updated_by},#{deleted_time},#{deleted_by},#{is_deleted},#{username},#{nickname},#{name},#{img_url},#{note},#{password},#{phone},#{email},#{need_changepwd},#{permissions},#{staff_id},#{ext_fields},#{pwd_updated_time},#{lang},#{theme},#{theme_color}   )")
    void userCreate(UserBean paramUserBean);

    @Update("UPDATE app.policy SET hits = ( SELECT hits FROM app.policy WHERE ID = #{id} ) + 1 WHERE ID = #{id}")
    void addhitsP(@Param("id") Integer id);

    @Update("UPDATE app.policy_elucidation SET hits = ( SELECT hits FROM app.policy_elucidation WHERE ID = #{id} ) + 1 WHERE ID = #{id}")
    void addhitsPe(@Param("id")Integer id);

    @Select("SELECT count(1) FROM app.collect WHERE user_id = #{user_id} and code = #{code} and is_deleted = 0 and enabled=1 ")
    int queryInfoCollect( @Param("user_id") Integer user_id,@Param("code") String code);

    @Select("SELECT count(1) FROM app.user_read WHERE user_id = #{user_id} and code = #{code} and is_deleted = 0 and enabled=1 ")
    int queryInfoRead( @Param("user_id") Integer user_id,@Param("code") String code);

}
